/**
 * Created by duwei on 2019/2/23.
 */
var Zipai_PlayerBackWindow = PlayBackWindow.extend({
    step: 0,
    curRound: 0,
    zongRound: 0,
    round: 0,//第几轮  快进快退  退的是轮
    lastround: 0,
    isPause: false,
    /**
     * 初始化工程资源
     * @private
     */
    _ccsFileName: res.Zipai_PlayerBackWindow_json,
    /**
     * 点击事件
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            'root.backward': '_clickBackward',
            'root.back': '_clickBack',
            'root.toward': '_clickToward',
            'root.btn_pause': '_clickPause',
            'root.btn_play': '_clickPlay',
            'root.nextRound': '_clickNextRound',
            'root.beforeRound': '_clickBeforeRound'
        };
    },
    /**
     * 快退
     * @private
     */
    _clickBackward: function () {
        var self = this;
        if (self.round > 0) {
            self._clickPause();
            self.round--;
            self.step = self.replayData.getStepByRound(self.round);
            self.step = parseInt(self.step) - 1;
            self.room.resetPlayerCards(this.round);
        }
        self.setBackTowardStatus();
    },
    /**
     * 点击返回
     * @private
     */
    _clickBack: function () {
        var self = this;
        showMessageBox('是否要退出当前回放?', function () {
            clearInterval(self.interval);
            network.start();
            showBoard('HallBoard');
        });
    },
    /**
     * 点击快进
     * @private
     */
    _clickToward: function () {
        var self = this;
        if (self.round < self.lastround) {
            this._clickPause();
            this.round++;
            this.step = self.replayData.getStepByRound(this.round);
            this.step = parseInt(this.step) - 1;
            this.room.resetPlayerCards(this.round);
        }
        this.setBackTowardStatus();
        if (this.round == this.lastround) {
            this.isPause = false;
        }
    },
    /**
     * 点击暂停
     * @private
     */
    _clickPause: function () {
        this.btn_play.setVisible(true);
        this.btn_pause.setVisible(false);
        this.isPause = true;
    },
    /**
     * 点击开始
     * @private
     */
    _clickPlay: function () {
        this.btn_play.setVisible(false);
        this.btn_pause.setVisible(true);
        this.isPause = false;
    },
    /**
     * 点击下一局
     * @private
     */
    _clickNextRound: function () {
        if (this.curRound >= this.zongRound) {
            return;
        }
        clearInterval(this.interval);
        network.start();
        this.step = 0;
        this.nextRoundPlay(true);
    },
    /**
     * 点击上一局
     * @private
     */
    _clickBeforeRound: function () {
        if (this.curRound <= 1) {
            return;
        }
        clearInterval(this.interval);
        network.start();
        this.step = 0;
        this.nextRoundPlay(false);
    },
    /**
     * 初始化界面
     * @param data
     * @private
     */
    _initPlayBackWindow: function (data) {
        var self = this;
        self.replayData = data;
        this.btn_backward = self._nodeList['root.backward'];
        this.btn_backward2 = self._nodeList['root.backward2'];
        this.btn_back = self._nodeList['root.back'];
        this.btn_toward = self._nodeList['root.toward'];
        this.btn_toward2 = self._nodeList['root.toward2'];
        this.btn_pause = self._nodeList['root.pause'];
        this.btn_play = self._nodeList['root.play'];
        this.nextRound = self._nodeList['root.nextRound'];
        this.beforeRound = self._nodeList['root.beforeRound'];

        this.btn_backward2.setVisible(false);
        this.btn_toward2.setVisible(false);
        this.lastround = self.replayData.getLastRound();
        this.curRound = parseInt(gameData.recordIds[1]);
        this.zongRound = gameData.totalRound;
        this.interval = setInterval(this.run.bind(this), 800);
        this.checkBtnVisible();
    },
    checkBtnVisible: function () {
        if (this.curRound <= 1) {
            this.beforeRound.setVisible(false);
        } else {
            this.beforeRound.setVisible(true);
        }
        if (this.curRound >= this.zongRound) {
            this.nextRound.setVisible(false);
        } else {
            this.nextRound.setVisible(true);
        }
    },
    run: function () {
        if (this.isPause) {
            return;
        }
        this.step++;
        if (this.replayData.isOver(this.step)) {
            clearInterval(this.interval);
        } else {
            this.round = this.replayData.getRound(this.step);
            this.replayData.runEvent(this.step);
        }
        this.changeTxtStep();
        this.setBackTowardStatus();
    },
    changeTxtStep: function () {
        var len = this.replayData.getDataLength();
        var percent = Math.floor(this.step * 10000 / len) / 100.0;
    },
    setBackTowardStatus: function () {
        if (!this.btn_toward) {
            return;
        }
        if (this.round == 0) {
            this.btn_toward.setVisible(true);
            this.btn_toward2.setVisible(false);
            this.btn_backward.setVisible(false);
            this.btn_backward2.setVisible(true);
        } else if (this.round == this.lastround) {
            this.btn_toward.setVisible(false);
            this.btn_toward2.setVisible(true);
            this.btn_backward.setVisible(true);
            this.btn_backward2.setVisible(false);
        } else {
            this.btn_toward.setVisible(true);
            this.btn_toward2.setVisible(false);
            this.btn_backward.setVisible(true);
            this.btn_backward2.setVisible(false);
        }
    },
    /**
     * 获取其他局回放
     * @param isNext
     */
    nextRoundPlay: function (isNext) {
        var self = this;
        var recordId = gameData.recordIds[0] + '-' + (!isNext ? (parseInt(gameData.recordIds[1]) - 1) : (parseInt(gameData.recordIds[1]) + 1));
        var str = (isNext ?'下一局':'上一局')+'回放';
        showLoading('正在获取'+str + '...');
        var replayurl = app.recordPath[1] + '/' + app.recordPath[2] + recordId;
        console.log('replayurl = ' + replayurl);
        gameData.recordIds = recordId.split('-');
        http.get(replayurl, function (data) {
            var tData = data.replace(/'/g, "\"");
            data = JSON.parse(tData);
            if (data['3002']) {
                gameData.mapId = data['3002']['data']['map_id'];
            } else {
                var option = JSON.parse(data[0]['data']['Option']);
                gameData.mapId = option['mapid'];
            }
            SubUpdateUtil.checkUpdateSubByMapId(gameData.mapId,function (sub) {
                showRoom(data, ROOM_DATA_TYPE.REPLAY);
            });
        }, function () {
            hideLoading();
            showMessageBox('未获取'+str + '数据,可能房间已解散。');
            self.zongRound = self.curRound;
            self.checkBtnVisible();
        });
    },
});